package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.modules.university.entity.UniversityBook;
import com.changxianggu.cloud.crawler.core.modules.university.service.IUniversityBookService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class BookSync {

    @Autowired
    ICxgBookService cxgBookService;

    @Autowired
    IUniversityBookService universityBookService;


    @Test
    public void toSync() {
        initSync();
    }

    public void initSync() {
        List<CxgBook> cxgBookList = cxgBookService.query()
                .eq(CxgBook::getIsSync, 0)
                .eq(CxgBook::getStatus, 1)
                .list();
        for (CxgBook cxgBook : cxgBookList) {
            String bookIsbn = cxgBook.getBookIsbn();
            log.info(bookIsbn);
            //当前时间戳
            long updateSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
            //出版时间
            LocalDateTime pubDate = cxgBook.getPubDate();
            int count = universityBookService.queryCountByIsbn(bookIsbn);
            UniversityBook universityBook = new UniversityBook();
            String cover = cxgBook.getCover();
            String digitalUri = cxgBook.getDigitalUri();
            String coursewareUri = cxgBook.getCoursewareUri();
            String author = cxgBook.getAuthor();
            if (count == 0) {
                //新增教材操作
                BeanUtil.copyProperties(cxgBook, universityBook);
                universityBook.setId(null);
                universityBook.setAddTime(updateSecond);
                universityBook.setUpdateTime(updateSecond);
                if (StrUtil.isNotBlank(digitalUri)) {
                    universityBook.setDigitalUri(digitalUri);
                    universityBook.setEbookType(1);
                    universityBook.setUpdateTime(updateSecond);
                } else {
                    universityBook.setDigitalUri("");
                    universityBook.setEbookType(0);
                    universityBook.setUpdateTime(null);
                }

                if (pubDate != null) {
                    universityBook.setPublishTime(pubDate.toEpochSecond(ZoneOffset.of("+8")));
                }

                if (StrUtil.isNotBlank(cover)) {
                    universityBook.setCover(cover);
                }
                BigDecimal price = cxgBook.getPrice();
                if (price == null) {
                    universityBook.setPrice(BigDecimal.valueOf(0));
                }
                if (StrUtil.isNotBlank(author) && author.length() > 20) {
                    universityBook.setAuthor(author.substring(0, 20));
                }
                universityBookService.saveEntity(universityBook);
            } else {

                universityBook.setBookIsbn(bookIsbn);
                if (StrUtil.isNotBlank(cover)) {
                    universityBook.setCover(cover);
                }
                if (StrUtil.isNotBlank(digitalUri)) {
                    universityBook.setDigitalUri(digitalUri);
                    universityBook.setEbookType(1);
                    universityBook.setUpdateTime(updateSecond);
                } else {
                    universityBook.setDigitalUri("");
                    universityBook.setEbookType(0);
                    universityBook.setUpdateTime(null);
                }
                if (StrUtil.isNotBlank(coursewareUri)) {
                    universityBook.setCoursewareUri(coursewareUri);
                    universityBook.setUpdateTime(updateSecond);
                }
                if (pubDate != null) {
                    universityBook.setPublishTime(pubDate.toEpochSecond(ZoneOffset.of("+8")));
                }
                if (StrUtil.isNotBlank(author) && author.length() > 20) {
                    universityBook.setAuthor(author.substring(0, 20));
                }

                universityBookService.updateEntityByIsbn(universityBook);
            }
            cxgBook.setIsSync(1);
            cxgBook.updateById();
        }
    }

    @Test
    public void getLongBookName() {

    }

    @Test
    public void cleanData() {
        List<CxgBook> repeatedBookList =  cxgBookService.selectRepeatedBook();
        for (CxgBook cb : repeatedBookList) {
            String bookIsbn = cb.getBookIsbn();
            Integer id = cb.getId();
            log.info(cb.getBookIsbn());
            cxgBookService.delete().eq(CxgBook::getBookIsbn, bookIsbn).notIn(CxgBook::getId, id).execute();
            log.info("--------");
        }
    }
}
